package com.tanhua.server.interceptor;

import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.dubbo.server.api.LogApi;
import com.tanhua.server.utils.UserThreadLocal;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * @Author: JinboYang
 * @Description:
 * @Date: Created in 下午 20:03 2021/1/17
 * @ModifiedBy:
 */
@Component
public class OperatorInterceptor implements HandlerInterceptor {

    @Reference(version = "1.0.0")
    private LogApi logApi;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String annotation = Arrays.toString(((HandlerMethod) handler).getMethod().getDeclaredAnnotations());
        this.logApi.save(UserThreadLocal.get().getId(),request.getRequestURI(), annotation);
        return true;
    }
}
